using System;
using Castle.Core.Interceptor;
using Microsoft.Practices.Unity;
using UniAspect.Common;

namespace UniAspect.Aspect
{
    /// <summary>
    /// Stub that imitates unloaded aspect
    /// </summary>
    public class UnresolvedAspect : IAspect
    {
        private readonly IUnityContainer _container;
        private readonly Type _aspectType;
        private readonly string _aspectName;

        private IAspect _loadedAspect;

        public UnresolvedAspect(IUnityContainer container, Type aspectType, string aspectKey)
        {
            _container = container;
            _aspectType = aspectType;
            _aspectName = aspectKey;
        }

        public void Intercept(IInvocation invocation)
        {
            if(_loadedAspect == null)
            {
                _loadedAspect = _container.Resolve(_aspectType, _aspectName).Cast<IAspect>();
            }
            _loadedAspect.Intercept(invocation);
        }

    }
}